package com.egreen.admin.controller;


import com.egreen.admin.service.IAdminService;
import com.egreen.admin.service.IOperationRecordService;
import com.egreen.admin.utils.AdminOperationRecord;
import com.egreen.admin.utils.JwtUtil;
import com.egreen.common.Result;
import com.egreen.common.States;
import com.egreen.common.pojo.DTO.system.*;
import com.egreen.common.pojo.VO.system.GetSystemAnnouncementVO;
import com.egreen.common.pojo.VO.system.GetSystemInformationVO;
import com.egreen.common.pojo.VO.system.GetUserFeedbackVO;
import com.egreen.common.pojo.entity.admin.Admin;
import com.egreen.common.pojo.entity.system.Commodity;
import com.egreen.common.pojo.DTO.user.loginaccount.LoginByPasswordDTO;
import com.egreen.common.serviceapi.alipay.IAlipayServiceApi;

import com.egreen.common.serviceapi.asset.IAssetServiceApi;
import com.egreen.common.serviceapi.order.IOrderCountServiceApi;
import com.egreen.common.serviceapi.order.IOrderServiceApi;
import com.egreen.common.serviceapi.system.ISystemAPI;
import com.egreen.common.serviceapi.user.IUserServiceApi;
import com.egreen.common.serviceapi.user.IAgentServiceApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Api("管理员相关接口")
@RestController
@RequestMapping("/admin")
public class AdminController {
    @DubboReference
    IOrderServiceApi orderServiceApi;
    @DubboReference
    ISystemAPI systemAPI;
    @Autowired
    IOperationRecordService operationRecordService;
    @Autowired
    JwtUtil jwtUtil;
    @Autowired
    IAdminService adminService;
    @DubboReference
    IAlipayServiceApi iAlipayServiceApi;
    @DubboReference
    IUserServiceApi iUserServiceApi;
    @DubboReference
    IAgentServiceApi agentServiceApi;
    @DubboReference
    IOrderCountServiceApi orderCountServiceApi;
    @DubboReference
    IAssetServiceApi assetServiceApi;
    @DubboReference
    IUserServiceApi userServiceApi;

    @GetMapping("/testLink")
    void testLink() {
        assetServiceApi.selectPointById(1L);
    }

    /**
     * 获取用户列表
     *
     * @param state
     * @return
     */
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表,根据用户状态查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "用户状态", required = true)
    })
    @GetMapping("/selectUserList")
    @AdminOperationRecord(message = "获取用户列表", type = "查找操作")
    Result selectUserList(@RequestParam() Integer state) {
        return new Result(userServiceApi.selectUserList(state), States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 兑换记录的查找
     *
     * @param
     * @return
     */
    /*@ApiOperation(value = "兑换记录的查找", notes = "兑换记录的查找,根据用户状态查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "订单状态", required = true),
    })
    @GetMapping("/selectExchangeRecords")
    @AdminOperationRecord(message = "查找兑换记录", type = "查找操作")
    Result selectExchangeRecords(@RequestParam() Integer state) {
        return new Result(systemAPI.selectExchangeRecords(state), States.OK.getStates(), States.OK.getDescription());
    }*/

    /**
     * 查询所有人的总余额
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询所有人的总余额", notes = "查询所有人的总余额,根据每个用户余额查找")
    @GetMapping("/selectTotalBalance")
    @AdminOperationRecord(message = "查询所有人的总余额", type = "查找操作")
    Result selectTotalBalance() {
        return new Result(assetServiceApi.selectTotalBalance(), States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 通过查找操作记录来获得已提现到支付宝的总额
     *
     * @param
     * @return
     */
    @ApiOperation(value = "通过查找操作记录来获得已提现到支付宝的总额", notes = "通过查找每个用户的操作记录来获得已提现到支付宝的总额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operate", value = "操作", required = true),
    })
    @GetMapping("/selectWithdrawalTotalBalance")
    @AdminOperationRecord(message = "通过查找操作记录来获得已提现到支付宝的总额", type = "查找操作")
    Result selectWithdrawalTotalBalance(@RequestParam() String operate) {
        return new Result(assetServiceApi.selectWithdrawalTotalBalance(operate), States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 管理员修改商品库存
     *
     * @param reduceStockDTO
     * @return
     */
    @ApiOperation(value = "修改库存", notes = "管理员修改商品库存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", required = true),
            @ApiImplicitParam(name = "remainingQuantity", value = "新的库存", required = true),
            @ApiImplicitParam(name = "ReduceStockDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/reduceStock")
    @AdminOperationRecord(message = "修改商品库存", type = "更改操作")
    Result reduceStock(@RequestBody ReduceStockDTO reduceStockDTO) {
        systemAPI.reduceStock(reduceStockDTO);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 管理员修改商品价格
     *
     * @param changePriceDTO
     * @return
     */
    @ApiOperation(value = "修改价格", notes = "管理员修改商品价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", required = true),
            @ApiImplicitParam(name = "price", value = "新的价格", required = true),
            @ApiImplicitParam(name = "ChangePriceDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/changPrice")
    @AdminOperationRecord(message = "修改商品价格", type = "更改操作")
    Result changPrice(@RequestBody ChangePriceDTO changePriceDTO) {
        systemAPI.changePrice(changePriceDTO);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 超级管理员查看管理员操作
     *
     * @return
     */
    @ApiOperation(value = "查看管理员操作", notes = "超级管理员查看管理员历史操作")
    @GetMapping("/queryOperationRecord")
    @AdminOperationRecord(message = "超级管理员查看了操作", type = "查看操作")
    Result queryoOerationRecord() {
        return new Result(operationRecordService.checkOpertion(), States.OK.getStates(), States.OK.getDescription());
    }

    /*
     * 查询系统公告
     * */
    @ApiOperation(value = "公告查询", notes = "通过标题查询公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "公告标题", required = true)
    })
    @GetMapping("/getSystemAnnouncement")
    @AdminOperationRecord(message = "查询系统公告", type = "查看操作")
    Result getSystemAnnouncement(@RequestParam(required = false) String title) {
        List<GetSystemAnnouncementVO> list = systemAPI.getSystemAnnouncement(title);
        return Result.OK(list);
    }

    /*
     * 添加系统公告
     * */
    @ApiOperation(value = "添加公告", notes = "添加系统公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "公告标题", required = true),
            @ApiImplicitParam(name = "content", value = "公告内容", required = true),
            @ApiImplicitParam(name = "AddSystemAnnouncementDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/addSystemAnnouncement")
    @AdminOperationRecord(message = "添加系统公告", type = "添加操作")
    Result addSystemAnnouncement(AddSystemAnnouncementDTO systemAnnouncementDTO) {
        systemAPI.addSystemAnnouncement(systemAnnouncementDTO);
        return Result.OK();
    }

    /*
     * 删除系统公告
     * */
    @ApiOperation(value = "删除公告", notes = "通过ID删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告ID", required = true)
    })
    @GetMapping("/deleteSystemAnnouncement")
    @AdminOperationRecord(message = "删除系统公告", type = "删除操作")
    Result deleteSystemAnnouncement(int id) {
        systemAPI.deleteSystemAnnouncement(id);
        return Result.OK();
    }

    /*
     * 删除兑换商品
     * */
    @ApiOperation(value = "删除商品", notes = "通过ID删除兑换商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告ID", required = true)
    })
    @GetMapping("/deletemerchaindise")
    @AdminOperationRecord(message = "删除兑换商品", type = "删除操作")
    Result deletemerchaindise(int id) {
        systemAPI.deletemerchaindise(id);
        return Result.OK();
    }

    /*
     * 查询系统消息
     * */
    @ApiOperation(value = "系统消息查询", notes = "通过标题查询系统消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "消息标题", required = true)
    })
    @GetMapping("/getSystemInformation")
    @AdminOperationRecord(message = "查询系统消息", type = "查看操作")
    Result getSystemInformation(@RequestParam String title) {
        List<GetSystemInformationVO> list = systemAPI.getSystemInformation(title);
        return Result.OK(list);
    }

    /*
     * 添加系统消息
     * */
    @ApiOperation(value = "添加系统消息", notes = "添加系统消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "消息标题", required = true),
            @ApiImplicitParam(name = "content", value = "消息内容", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(name = "adminId", value = "管理员ID", required = true),
            @ApiImplicitParam(name = "systemInformationDTO", value = "以上四个参数封装在此DTO中")
    })
    @GetMapping("/addSystemInformation")
    @AdminOperationRecord(message = "添加系统消息", type = "添加操作")
    Result addSystemInformation(SystemInformationDTO systemInformationDTO) {
        systemAPI.addSystemInformation(systemInformationDTO);
        return Result.OK();
    }

    /*
     * 添加兑换商品
     * */
    @ApiOperation(value = "添加兑换商品", notes = "添加兑换商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "商品名称", required = true),
            @ApiImplicitParam(name = "price", value = "消息价格", required = true),
            @ApiImplicitParam(name = "remainingQuantity", value = "商品库存", required = true),
            @ApiImplicitParam(name = "url", value = "图片地址", required = true),
            @ApiImplicitParam(name = "AddCommodityDTO", value = "以上四个参数封装在此DTO中")
    })
    @PostMapping("/addMerchandise")
    @AdminOperationRecord(message = "添加兑换商品", type = "添加操作")
    Result addMerchandise(@RequestBody AddCommodityDTO addCommodityDTO) {
        systemAPI.addMerchandise(addCommodityDTO);
        return Result.OK();
    }

    /*
     * 删除系统消息
     * */
    @ApiOperation(value = "删除系统消息", notes = "通过ID删除系统消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息ID", required = true)
    })
    @GetMapping("/deleteSystemInformation")
    @AdminOperationRecord(message = "删除系统消息", type = "删除操作")
    Result deleteSystemInformation(int id) {
        systemAPI.deleteSystemInformation(id);
        return Result.OK();
    }

    /*
     * 查询用户反馈消息
     * */
    @ApiOperation(value = "用户反馈消息查询", notes = "通过标题查询用户反馈消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "用户反馈消息标题", required = false)
    })
    @GetMapping("/getUserFeedback")
    @AdminOperationRecord(message = "查询用户反馈消息", type = "查询操作")
    Result getUserFeedback(@RequestParam(required = false) String title) {
        List<GetUserFeedbackVO> list = systemAPI.getUserFeedback(title);
        return Result.OK(list);
    }

    /**
     * 管理员登录
     *
     * @param loginByPasswordDTO
     * @return
     */
    @PostMapping("/adminLogin")
    Result adminLogin(@RequestBody LoginByPasswordDTO loginByPasswordDTO) {
        Admin adminlogin = adminService.Adminlogin(loginByPasswordDTO);
        //生成token
        String token = jwtUtil.createTokenWhenLogin(adminlogin.getId(), adminlogin.getStatus());
        return Result.OK(new String[]{token, String.valueOf(
                adminlogin.getId()), String.valueOf(adminlogin.getStatus())});
    }

    /**
     * 查询商家支付宝余额
     */
    @ApiOperation(value = "查询商家支付宝余额", notes = "查询商家支付宝余额")
    @GetMapping("/queryBusBalance")
    @AdminOperationRecord(message = "查询商家支付宝余额", type = "查询操作")
    Result queryBusBalance() {
        String availableAmount = iAlipayServiceApi.queryBusBalance();
        if (availableAmount == "-1") {
            return Result.Error(States.ERR_ALIPAY_SERVER);
        } else {
            return Result.OK(availableAmount);
        }
    }

    /**
     * 更新物流信息
     */
    /*@ApiOperation(value = "更新物流信息", notes = "更新物流信息")
    @GetMapping("/updateLogistics")
    @AdminOperationRecord(message = "管理员更新物流消息", type = "更新操作")
    Result updateLogistics(@RequestBody LogisticsInformationDTO logisticsInformationDTO) {
        systemAPI.updateLogistics(logisticsInformationDTO);
        return Result.OK();
    }*/

    /**
     * 根据价格区间查找商品
     */
    /*@ApiOperation(value = "根据价格区间查找商品", notes = "根据价格区间查找商品")
    @GetMapping("/queryProjectByRange")
    @AdminOperationRecord(message = "根据价格区间查询了商品", type = "查询操作")
    Result queryProjectByRange(@RequestBody ProductPriceRangeDTO productPriceRangeDTO) {
        List<Commodity> commodityList = systemAPI.queryProjectByRange(productPriceRangeDTO);
        return Result.OK(commodityList);
    }*/

    /**
     * 更改用户状态
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "更改用户状态", notes = "根据用户id更改用户状态status为0（普通用户）")
    @GetMapping("/changeUserStatus")
    @AdminOperationRecord(message = "更改用户状态", type = "更改操作")
    Result changeUserStatus(@RequestBody int id) {
        iUserServiceApi.changeUserStatus(id);
        return Result.OK();
    }


    @ApiOperation(value = "代理申请信息查看", notes = "可通过state查询审核中，通过，拒绝的申请，代码分别是0，1，2，" +
            "不传入该值则查询全部")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "申请状态", required = false)
    })
    @GetMapping("/getAgentApplicationList")
    @AdminOperationRecord(message = "查询申请信息", type = "查看操作")
    Result getAgentApplicationList(@RequestParam(value = "state", required = false) Integer state) {
        return Result.OK(agentServiceApi.getAgentApplicationList(state));
    }


    @ApiOperation(value = "通过代理申请", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "申请表id", required = true)
    })
    @GetMapping("/passAgentApplication")
    @AdminOperationRecord(message = "通过代理申请", type = "修改操作")
    Result passAgentApplication(@RequestParam Long id) {
        agentServiceApi.passAgentApplication(id);
        return Result.OK();
    }


    @ApiOperation(value = "拒绝代理申请", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "申请表id", required = true)
    })
    @GetMapping("/refuseAgentApplication")
    @AdminOperationRecord(message = "拒绝代理申请", type = "修改操作")
    Result refuseAgentApplication(@RequestParam Long id) {
        agentServiceApi.refuseAgentApplication(id);
        return Result.OK();
    }

    @ApiOperation(value = "获取今日订单统计", notes = "传入日期格式：yyyy-MM-DD,返回格式：总订单，进行中订单，已完成订单。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "日期", required = true)
    })
    @GetMapping("/getOrderCountDay")
    Result getOrderCountDay(@RequestParam(value = "date") String date) {
        return Result.OK(orderCountServiceApi.getOrderCountDay(date));
    }

    @ApiOperation(value = "获取几日的订单统计", notes = "传入日期格式：yyyy-MM-DD数组,返回格式：数组 总订单，进行中订单，已完成订单。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dates", value = "日期数组", required = true)
    })
    @GetMapping("/getOrderCountDays")
    Result getOrderCountDays(@RequestParam(value = "dates") String[] dates) {
        return Result.OK(orderCountServiceApi.getOrderCountDays(dates));
    }

    @ApiOperation(value = "获取订单总量", notes = "返回格式：总订单")
    @GetMapping("/getOrderAll")
    Result getOrderAll() {
        return Result.OK(orderCountServiceApi.getOrderAll());
    }
}

